﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Providers;
using System.Linq;
using BookStoreService.Core.Models;
using BookStoreService.Core.Providers;
using BookStoreService.Core.Tools;

namespace BookStoreService.Core
{
    // We use custom data provider
    public class BookStoreDataServiceCore : DataService<BookStore>, IServiceProvider
    {
        protected override BookStore CreateDataSource()
        {
            var authors = new List<Author> 
                {new Author("Mark"), new Author("Sarit"), new Author("John")};

            var repository = RepositoryBuilder.Create<Book>("Books.xml");
            if (repository.Query().ToList().Count == 0)
            {
                repository.Add(new Book("Book1", "", new List<Author>()));
                repository.Add(new Book("Book2", "", new List<Author>()));
            }

            return new BookStore(repository, authors);
        }

        public object GetService(Type serviceType)
        {
            // We provide only update services..
            if (serviceType == typeof(IDataServiceUpdateProvider))
                return new BookStoreUpdateProvider(CurrentDataSource);

            return null;
        }
    }
}
